Action, Magic - February 10, 2025

Unity FPS Controller

Unity FPS Controller

FPS (or First-Person Shooter) is a game where the main character is controlled from a first-person perspective.

Steps to create an FPS Controller:

To make an FPS controller, follow the steps below:

Step 1: Create FPSPlayer

Create a new Game Object (GameObject -> Create Empty) and name it "FPSPlayer".

Step 2: Add a Capsule

Create a new Capsule (GameObject -> 3D Object -> Capsule) and move it inside the "FPSPlayer" Object.

Step 3: Remove Capsule Collider

Remove the Capsule Collider component from the Capsule and change its position to (0, 1, 0).

Step 4: Adjust Camera

Move the Main Camera inside the "FPSPlayer" Object and change its position to (0, 1.64, 0).

Step 5: Add Script

Create a new script, name it "SC_FPSController", and paste the code below inside it:

using System.Collections; using System.Collections.Generic; using UnityEngine; [RequireComponent(typeof(CharacterController))] public class SC_FPSController : MonoBehaviour { public float walkingSpeed = 7.5f; public float runningSpeed = 11.5f; public float jumpSpeed = 8.0f; public float gravity = 20.0f; public Camera playerCamera; public float lookSpeed = 2.0f; public float lookXLimit = 45.0f; CharacterController characterController; Vector3 moveDirection = Vector3.zero; float rotationX = 0; [HideInInspector] public bool canMove = true; void Start() { characterController = GetComponent(); Cursor.lockState = CursorLockMode.Locked; Cursor.visible = false; } void Update() { Vector3 forward = transform.TransformDirection(Vector3.forward); Vector3 right = transform.TransformDirection(Vector3.right); bool isRunning = Input.GetKey(KeyCode.LeftShift); float curSpeedX = canMove ? (isRunning ? runningSpeed : walkingSpeed) * Input.GetAxis("Vertical") : 0; float curSpeedY = canMove ? (isRunning ? runningSpeed : walkingSpeed) * Input.GetAxis("Horizontal") : 0; float movementDirectionY = moveDirection.y; moveDirection = (forward * curSpeedX) + (right * curSpeedY); if (Input.GetButton("Jump") && canMove && characterController.isGrounded) { moveDirection.y = jumpSpeed; } else { moveDirection.y = movementDirectionY; } if (!characterController.isGrounded) { moveDirection.y -= gravity * Time.deltaTime; } characterController.Move(moveDirection * Time.deltaTime); if (canMove) { rotationX += -Input.GetAxis("Mouse Y") * lookSpeed; rotationX = Mathf.Clamp(rotationX, -lookXLimit, lookXLimit); playerCamera.transform.localRotation = Quaternion.Euler(rotationX, 0, 0); transform.rotation *= Quaternion.Euler(0, Input.GetAxis("Mouse X") * lookSpeed, 0); } } }

Final Steps

Attach the SC_FPSController script to the "FPSPlayer" Object (it will also add a Character Controller component; change its center value to (0, 1, 0)).

Assign the Main Camera to the Player Camera variable in SC_FPSController.

Conclusion

The FPS controller is now ready! You can test it and tweak settings for better performance.

3 Comments

Sep 08, 2020
John Smith

Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi

Like Reply
Sep 08, 2020
Elizabeth Perry

Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi

Like Reply
Sep 08, 2020
Adrian Coleman

Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi

Like Reply

Leave A Commnet